<!DOCTYPE HTML>
<!--
	Dimension by HTML5 UP
	html5up.net | @ajlkn
	Free for personal and commercial use under the CCA 3.0 license (html5up.net/license)
-->
<html>
 <head>
  <title>
   Dimension by HTML5 UP
  </title>
  <!-- <meta charset="utf-8" /> -->
  <!-- <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" /> -->
  <meta charset="utf-8"/>
  <meta content="width=device-width,initial-scale=1.0" name="viewport"/>
  <link href="../../assets/css/article.css" rel="stylesheet"/>
  <link href="https://cdn.bootcss.com/highlight.js/9.15.8/styles/github.min.css" rel="stylesheet"/>
  <noscript>
   <link href="../../assets/css/noscript.css" rel="stylesheet"/>
  </noscript>
 </head>
 <body>
  <div id="app">
  </div>
  <!-- built files will be auto injected -->
 </body>
 <body class="is-preload">
  <!-- Wrapper -->
  <div id="wrapper">
   <!-- Main -->
   <div id="main">
    <article id="article">
     <h1 id="mongodb">
      MongoDB 使用记录
     </h1>
     <hr/>
     <h2 id="_1">
      安装配置
     </h2>
     <div class="codehilite">
      <pre><span></span><code>vim /etc/yum.repos.d/mongodb-org-4.0.repo

<span class="o">[</span>mongodb-org-4.0<span class="o">]</span>
<span class="nv">name</span><span class="o">=</span>MongoDB Repository
<span class="nv">baseurl</span><span class="o">=</span>https://repo.mongodb.org/yum/redhat/<span class="nv">$releasever</span>/mongodb-org/4.0/x86_64/
<span class="nv">gpgcheck</span><span class="o">=</span><span class="m">1</span>
<span class="nv">enabled</span><span class="o">=</span><span class="m">1</span>
<span class="nv">gpgkey</span><span class="o">=</span>https://www.mongodb.org/static/pgp/server-4.0.asc

yum install -y mongodb-org

service mongod start

<span class="c1"># 修改能被外界访问</span>
vim /etc/mongodb.conf

<span class="c1"># 启动和开机启动设置</span>
systemctl start mongod
systemctl <span class="nb">enable</span> mongod
</code></pre>
     </div>
     <h2 id="_2">
      操作
     </h2>
     <h3 id="collection">
      删除collection
     </h3>
     <div class="codehilite">
      <pre><span></span><code>mongo
&gt;use <span class="nb">test</span>
switched to db <span class="nb">test</span>
&gt; show collections
mycol
mycollection
newcollection
&gt;db.mycollection.drop<span class="o">()</span>
&gt;db.users.find<span class="o">()</span>.count<span class="o">()</span>
&gt;db.col.createIndex<span class="o">({</span><span class="s2">"description"</span>:-1<span class="o">})</span>
<span class="nb">true</span>
</code></pre>
     </div>
     <h3 id="_3">
      排序
     </h3>
     <h3 id="_4">
      索引
     </h3>
     <div class="codehilite">
      <pre><span></span><code><span class="c1"># 语法中 Key 值为你要创建的索引字段，1 为指定按升序创建索引，如果你想按降序来创建索引指定为 -1 即可。</span>
db.collection.createIndex<span class="o">(</span>keys, options<span class="o">)</span>

<span class="c1"># 查看集合索引</span>
db.col.getIndexes<span class="o">()</span>

<span class="c1"># 查看集合索引大小</span>
db.col.totalIndexSize<span class="o">()</span>

<span class="c1"># 删除集合所有索引</span>
db.col.dropIndexes<span class="o">()</span>

<span class="c1"># 删除集合指定索引</span>
db.col.dropIndex<span class="o">(</span><span class="s2">"索引名称"</span><span class="o">)</span>
</code></pre>
     </div>
     <h3 id="_5">
      去重统计
     </h3>
     <div class="codehilite">
      <pre><span></span><code>db.collection_name.distinct<span class="o">(</span>field,query,options<span class="o">)</span>
db.collection_name.distinct<span class="o">(</span>field,query,options<span class="o">)</span>.length

field -----指定要返回的字段<span class="o">(</span>string<span class="o">)</span>
query-----条件查询<span class="o">(</span>document<span class="o">)</span>
</code></pre>
     </div>
     <h3 id="_6">
      不同数据库之间表复制
     </h3>
     <div class="codehilite">
      <pre><span></span><code>db.threat.find<span class="o">({</span><span class="s1">'unit_id'</span>: <span class="s1">'90000001'</span><span class="o">})</span>.forEach<span class="o">(</span><span class="k">function</span><span class="o">(</span>d<span class="o">){</span> db.getSiblingDB<span class="o">(</span><span class="s1">'test_center'</span><span class="o">)[</span><span class="s1">'threat'</span><span class="o">]</span>.insert<span class="o">(</span>d<span class="o">)</span><span class="p">;</span> <span class="o">})</span><span class="p">;</span>
db.comprehensiveIndexTest.find<span class="o">()</span>.forEach<span class="o">(</span><span class="k">function</span><span class="o">(</span>d<span class="o">){</span> db.getSiblingDB<span class="o">(</span><span class="s1">'my_visual'</span><span class="o">)[</span><span class="s1">'comprehensiveIndex'</span><span class="o">]</span>.insert<span class="o">(</span>d<span class="o">)</span><span class="p">;</span> <span class="o">})</span><span class="p">;</span>
db.basedIndexTest.find<span class="o">()</span>.forEach<span class="o">(</span><span class="k">function</span><span class="o">(</span>d<span class="o">){</span> db.getSiblingDB<span class="o">(</span><span class="s1">'my_visual'</span><span class="o">)[</span><span class="s1">'basedIndex'</span><span class="o">]</span>.insert<span class="o">(</span>d<span class="o">)</span><span class="p">;</span> <span class="o">})</span><span class="p">;</span>
db.fragileIndexTest.find<span class="o">()</span>.forEach<span class="o">(</span><span class="k">function</span><span class="o">(</span>d<span class="o">){</span> db.getSiblingDB<span class="o">(</span><span class="s1">'my_visual'</span><span class="o">)[</span><span class="s1">'fragileIndex'</span><span class="o">]</span>.insert<span class="o">(</span>d<span class="o">)</span><span class="p">;</span> <span class="o">})</span><span class="p">;</span>
db.threadIndexTest.find<span class="o">()</span>.forEach<span class="o">(</span><span class="k">function</span><span class="o">(</span>d<span class="o">){</span> db.getSiblingDB<span class="o">(</span><span class="s1">'my_visual'</span><span class="o">)[</span><span class="s1">'threadIndex'</span><span class="o">]</span>.insert<span class="o">(</span>d<span class="o">)</span><span class="p">;</span> <span class="o">})</span><span class="p">;</span>
</code></pre>
     </div>
     <h3 id="_7">
      数据库复制
     </h3>
     <div class="codehilite">
      <pre><span></span><code>db.copyDatabase<span class="o">(</span><span class="s2">"phone_record"</span>,<span class="s2">"self_growth"</span>,<span class="s2">"10.9.72.82:27017"</span><span class="o">)</span><span class="p">;</span>
</code></pre>
     </div>
     <h3 id="sortsize">
      sort()方法的size限制
     </h3>
     <div class="codehilite">
      <pre><span></span><code>db.adminCommand<span class="o">({</span>setParameter:1, internalQueryExecMaxBlockingSortBytes:335544320<span class="o">})</span>
</code></pre>
     </div>
     <h3 id="_8">
      模糊查询
     </h3>
     <div class="codehilite">
      <pre><span></span><code>db.getCollection<span class="o">(</span><span class="s1">'unit'</span><span class="o">)</span>.find<span class="o">({</span><span class="s2">"target"</span>:/192.168.169.0.24.*/<span class="o">})</span>
</code></pre>
     </div>
     <h3 id="_9">
      导入导出
     </h3>
     <div class="codehilite">
      <pre><span></span><code>mongoimport -h 我的IP --port 端口 -d 数据库名 -c 表名 -u 帐号 -p 密码 --upsert /usr/share/nginx/html/xxx.json --authenticationDatabase admin

mongorestore -d db_name 文件夹目录

<span class="c1"># 导入json</span>
mongoimport -h <span class="m">172</span>.19.104.18 --port <span class="m">20017</span> -d testdb -c testcol -u mytest -p <span class="m">12345</span> --upsert /tmp/test.json --authenticationDatabase testdb

<span class="c1"># 导出数据库</span>
mongodump -h <span class="m">172</span>.19.104.18 --port <span class="m">20017</span> -u mytest -p <span class="m">12345</span> -d testdb -o /tmp --authenticationDatabase testdb
docker <span class="nb">exec</span> -ti mongo mongodump -h <span class="m">127</span>.0.0.1 --port <span class="m">27017</span> -d phone_record -o /home/mongodb/
docker cp mongo:/home/mongodb/ /root/mongo/
docker cp /root/mongo/ mongodb:/home/mongodb/
docker <span class="nb">exec</span> -ti mongodb mongorestore -h <span class="m">127</span>.0.0.1 --port <span class="m">27017</span> -u <span class="s2">"mongoadmin"</span> -p <span class="s2">"secret"</span> --authenticationDatabase admin -d phone_record /home/mongodb/
docker <span class="nb">exec</span> -ti mongodb mongorestore --username <span class="s2">"mongoadmin"</span> --password <span class="s2">"secret"</span> --authenticationDatabase phone_record --db phone_record /home/mongodb/

<span class="c1"># 导出表</span>
mongoexport -h <span class="m">172</span>.19.104.18 --port <span class="m">20017</span> -u mytest -p <span class="m">12345</span> -d testdb -c testcol -o /tmp/test.json --authenticationDatabase testdb
</code></pre>
     </div>
     <h2 id="_10">
      参考链接
     </h2>
     <ul>
      <li>
       <a href="https://robomongo.org/">
        https://robomongo.org/
       </a>
      </li>
      <li>
       <a href="https://www.yiibai.com/mongodb/mongodb_drop_collection.html">
        MongoDB删除集合
       </a>
      </li>
      <li>
       <a href="http://www.runoob.com/mongodb/mongodb-query.html">
        MongoDB 查询文档
       </a>
      </li>
      <li>
       <a href="https://www.jianshu.com/p/c43d7cf49052">
        MongoDB更换数据库路径
       </a>
      </li>
      <li>
       <a href="https://blog.csdn.net/skh2015java/article/details/55667829">
        MongoDB中distinct的详细用法
       </a>
      </li>
      <li>
       <a href="https://blog.csdn.net/wulex/article/details/83479516">
        MongoDB在不同主机间复制数据库和集合
       </a>
      </li>
      <li>
       <a href="https://blog.csdn.net/hh12211221/article/details/78900098">
        mongodb——复制数据库和表
       </a>
      </li>
      <li>
       <a href="https://blog.csdn.net/zhouyan8603/article/details/6825834">
        mongodb 数据库like语句查询
       </a>
      </li>
      <li>
       <a href="https://hub.docker.com/_/mongo">
        docker mongo
       </a>
      </li>
      <li>
       <a href="https://studio3t.com/download/">
        Download Studio 3T for MongoDB
       </a>
      </li>
      <li>
       <a href="https://robomongo.org/download">
        Robo 3T
       </a>
      </li>
      <li>
       <a href="https://www.cnblogs.com/kevingrace/p/8184087.html">
        MongoDB日常运维操作命令小结
       </a>
      </li>
      <li>
       <a href="https://segmentfault.com/a/1190000006236494">
        mongodb 备份、还原、导入、导出简单操作
       </a>
      </li>
      <li>
       <a href="https://blog.sunriseydy.top/technology/linux/docker-install-mongodb/">
        使用 Docker 安装 MongoDB
       </a>
      </li>
     </ul>
    </article>
   </div>
   <!-- Footer -->
   <footer id="footer">
    <p class="copyright">
     © Untitled. Design:
     <a href="https://html5up.net">
      HTML5 UP
     </a>
     .
    </p>
   </footer>
  </div>
  <!-- BG -->
  <div id="bg">
  </div>
  <!-- Scripts -->
  <script src="../assets/js/jquery.min.js">
  </script>
  <script src="../assets/js/browser.min.js">
  </script>
  <script src="../assets/js/breakpoints.min.js">
  </script>
  <script src="../assets/js/util.js">
  </script>
  <script src="../assets/js/main.js">
  </script>
 </body>
</html>
